package com.itheima.springboot_reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.springboot_reggie.dao.CategoryDao;
import com.itheima.springboot_reggie.emtity.Category;
import com.itheima.springboot_reggie.emtity.Dish;
import com.itheima.springboot_reggie.emtity.Setmeal;
import com.itheima.springboot_reggie.service.ICategoryService;
import com.itheima.springboot_reggie.service.IDishService;
import com.itheima.springboot_reggie.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//新增菜品分类 新增套餐分类
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements ICategoryService {

    @Autowired
    CategoryDao categoryDao;

    @Autowired
    IDishService dishService;

    @Autowired
    ISetmealService setmealService;

    @Override
    public boolean deleteById(Long id) {
        //首先判断下当前分类的类型是什么（操作分类表）
        Category category = this.getById(id);

        //如果是1，表示菜品分类，接下来查询dish（菜品表）
        //1.1查询dish（菜品表）：select count(*) from dish where category_id =?(
        //1.2 查询的数量大于0，直接返回false
        if (category.getType()==1){//如果是1，表示菜品分类
            LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
            // 到MybatisPlus官网中查条件构造器，gt是大于

            dishWrapper.eq(Dish::getCategoryId,id);
            int count = dishService.count(dishWrapper);
            if (count>0){
                return false;
            }
        }

        //如果是2，表示套餐分类 , 接下来查询setmeal(套餐表)
        //2.1查询setmeal(套餐表)：select count(*) from dish where category_id =?
        //2.2 查询的数量大于0，直接返回false (count>0，说明菜品里面有定西，不能删除)
        if (category.getType()==2){//如果是2，表示套餐分类
            LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
            setmealWrapper.eq(Setmeal::getCategoryId,id);
            int count = setmealService.count(setmealWrapper);
            if (count>0){
                return false;
            }
        }

        //3.执行删除操作
        //若上面操作都没走，直接进行删除操作  delete from category where id=
        return true;
    }
}
  /*  *//** * 根据id删除分类，删除之前需要进行判断 *//*
    @Override
    public void remove(Long id) {  
        //1. 添加查询条件，根据分类id进行查询菜品数据  
        QueryWrapper<Dish> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_id", id);
        int count1 = dishService.count(queryWrapper1);
        //如果已经关联，抛出一个业务异常  
        if (count1 > 0) {
            throw new CustomerException("当前分类下关联了菜品，不能删除");
        //已经关联菜品，抛出一个业务异常    }​  
            // 2. 查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常  
            QueryWrapper<Setmeal> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("category_id", id);
            int count2 = setmealService.count(queryWrapper2);
            //如果已经关联，抛出一个业务异常  
            // if (count2 > 0) {      
            throw new CustomerException("当前分类下关联了套餐，不能删除");//已经关联套餐，抛出一个业务异常    }​  
            // 3. 正常删除分类  
            this.removeById(id);}
}
*/